/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 */
package org.emergent.antbite.savant;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * <p>
 * This class stores common helper methods for dealing with
 * files.
 * </p>
 *
 * @author Brian Pontarelli
 */
public class FileTools {

    /**
     * Reads from the given input stream and writes the contents out to the given
     * file.
     *
     * @param   is The InputStream to read from. This InputStream is wrapped in
     *          a BufferedInputStream for performance.
     * @param   output The file to output to.
     * @throws  IOException If the output operation fails.
     */
    public static void output(InputStream is, File output)
    throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(new FileOutputStream(output));
            byte[] b = new byte[1024];
            int len;
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }
        } finally {
            if (bis != null) {
                bis.close();
            }

            if (bos != null) {
                bos.close();
            }
        }
    }
}
